/* ==========================================
 * JGraphT : a free Java graph-theory library
 * ==========================================
 *
 * Project Info:  http://jgrapht.sourceforge.net/
 * Project Creator:  Barak Naveh (http://sourceforge.net/users/barak_naveh)
 *
 * (C) Copyright 2003-2008, by Barak Naveh and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/* -------------------------
 * MaskSubgraph.java
 * -------------------------
 * (C) Copyright 2007-2008, by France Telecom
 *
 * Original Author:  Guillaume Boulmier and Contributors.
 *
 * $Id: MaskSubgraph.java 1258 2013-04-18 20:27:18Z freecode4susant $
 *
 * Changes
 * -------
 * 05-Jun-2007 : Initial revision (GB);
 *
 */

package org.jgrapht.graph;

import org.jgrapht.DirectedGraph;
import org.jgrapht.EdgeFactory;
import org.jgrapht.Graph;
import org.jgrapht.UndirectedGraph;

import java.util.Collection;
import java.util.Set;

/**
 * An unmodifiable subgraph induced by a vertex/edge masking function. The
 * subgraph will keep track of edges being added to its vertex subset as well as
 * deletion of edges and vertices. When iterating over the vertices/edges, it
 * will iterate over the vertices/edges of the base graph and discard
 * vertices/edges that are masked (an edge with a masked extremity vertex is
 * discarded as well).
 *
 * @author Guillaume Boulmier
 * @since July 5, 2007
 * @param <V>
 * @param <E>
 */
public class MaskSubgraph<V, E> extends AbstractGraph<V, E> {

    private static final String UNMODIFIABLE = "this graph is unmodifiable";
    private final Graph<V, E> base;
    private final Set<E> edges;
    private final MaskFunctor<V, E> mask;
    private final Set<V> vertices;

    /**
     * Creates a new induced subgraph. Running-time = O(1).
     *
     * @param base the base (backing) graph on which the subgraph will be based.
     * @param mask vertices and edges to exclude in the subgraph. If a
     * vertex/edge is masked, it is as if it is not in the subgraph.
     */
    public MaskSubgraph(final Graph<V, E> base, final MaskFunctor<V, E> mask) {

        super();

        this.base     = base;
        this.mask     = mask;
        this.vertices = new MaskVertexSet<>(base.vertexSet(), mask);
        this.edges    = new MaskEdgeSet<>(base, base.edgeSet(), mask);
    }

    /**
     * @see Graph#addEdge(Object, Object)
     */
    public E addEdge(final V sourceVertex, final V targetVertex) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    public boolean addEdge(final V sourceVertex, final V targetVertex, final E edge) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    /**
     * @see Graph#addVertex(Object)
     */
    public boolean addVertex(final V v) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    public boolean containsEdge(final E e) {
        return edgeSet().contains(e);
    }

    public boolean containsVertex(final V v) {
        return !this.mask.isVertexMasked(v) && this.base.containsVertex(v);
    }

    /**
     * @see UndirectedGraph#degreeOf(Object)
     */
    public int degreeOf(final V vertex) {
        return edgesOf(vertex).size();
    }

    public Set<E> edgeSet() {
        return this.edges;
    }

    public Set<E> edgesOf(final V vertex) {

        assertVertexExist(vertex);

        return new MaskEdgeSet<>(this.base, this.base.edgesOf(vertex), this.mask);
    }

    public Set<E> getAllEdges(final V sourceVertex, final V targetVertex) {

        if (containsVertex(sourceVertex) && containsVertex(targetVertex)) {

            return new MaskEdgeSet<>(this.base, this.base.getAllEdges(sourceVertex, targetVertex),
                                     this.mask);
        }

        final Set<E> edges = null;

        return edges;
    }

    public E getEdge(final V sourceVertex, final V targetVertex) {

        final Set<E> edges = getAllEdges(sourceVertex, targetVertex);

        if ((edges == null) || edges.isEmpty()) {
            return null;
        } else {
            return edges.iterator().next();
        }
    }

    public EdgeFactory<V, E> getEdgeFactory() {
        return this.base.getEdgeFactory();
    }

    public V getEdgeSource(final E edge) {

        assert(edgeSet().contains(edge));

        return this.base.getEdgeSource(edge);
    }

    public V getEdgeTarget(final E edge) {

        assert(edgeSet().contains(edge));

        return this.base.getEdgeTarget(edge);
    }

    public double getEdgeWeight(final E edge) {

        assert(edgeSet().contains(edge));

        return this.base.getEdgeWeight(edge);
    }

    /**
     * @see DirectedGraph#incomingEdgesOf(Object)
     */
    public Set<E> incomingEdgesOf(final V vertex) {

        assertVertexExist(vertex);

        return new MaskEdgeSet<>(this.base,
                                 ((DirectedGraph<V, E>) this.base).incomingEdgesOf(vertex),
                                 this.mask);
    }

    /**
     * @see DirectedGraph#inDegreeOf(Object)
     */
    public int inDegreeOf(final V vertex) {
        return incomingEdgesOf(vertex).size();
    }

    /**
     * @see DirectedGraph#outDegreeOf(Object)
     */
    public int outDegreeOf(final V vertex) {
        return outgoingEdgesOf(vertex).size();
    }

    /**
     * @see DirectedGraph#outgoingEdgesOf(Object)
     */
    public Set<E> outgoingEdgesOf(final V vertex) {

        assertVertexExist(vertex);

        return new MaskEdgeSet<>(this.base,
                                 ((DirectedGraph<V, E>) this.base).outgoingEdgesOf(vertex),
                                 this.mask);
    }

    /**
     * @see Graph#removeAllEdges(Collection)
     */
    public boolean removeAllEdges(final Collection<? extends E> edges) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    /**
     * @see Graph#removeAllEdges(Object, Object)
     */
    public Set<E> removeAllEdges(final V sourceVertex, final V targetVertex) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    /**
     * @see Graph#removeAllVertices(Collection)
     */
    public boolean removeAllVertices(final Collection<? extends V> vertices) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    /**
     * @see Graph#removeEdge(Object)
     */
    public boolean removeEdge(final E e) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    /**
     * @see Graph#removeEdge(Object, Object)
     */
    public E removeEdge(final V sourceVertex, final V targetVertex) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    /**
     * @see Graph#removeVertex(Object)
     */
    public boolean removeVertex(final V v) {
        throw new UnsupportedOperationException(UNMODIFIABLE);
    }

    public Set<V> vertexSet() {
        return this.vertices;
    }
}

// End MaskSubgraph.java



